# 业务层
# 调用逻辑层的函数

# 导入Flask
from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
from flask import json
from flask import make_response
from flask import redirect
from flask import session
from flask import url_for

# from DataLayer import UserDataLoad
from LogicalLayer import LoginAndRegister
from LogicalLayer import Customer
from LogicalLayer import Waiter

app = Flask(__name__)

app.secret_key='abcdefg'

# @app.before_request
# def before_request(*args,**kwargs):
#     if request.path=='/login':
#         return None
#     str_no=request.path.split('/')[-1]
#     account = session.get('user_info')
#     if account==str_no:
#         return None
#     return redirect(url_for('login'))

@app.route('/store/login',methods=['GET','POST'])
def login():
    if request.method=="GET":
        return render_template('Login.html')
    else:
        # print(request.values)
        func=request.form.get('func')
        if func=='login':
            account = request.form.get('account')
            pwd = request.form.get('pwd')
            type = LoginAndRegister.login(account, pwd)
            if type == -1:
                # 解决跨域问题
                res=make_response(jsonify( {'type':'error','info':'该账户名不存在'}))
                res.headers['Access-Control-Allow-Origin'] = '*'
                res.headers['Access-Control-Allow-Method'] = '*'
                res.headers['Access-Control-Allow-Headers'] = '*'
                return res
            elif type == 0:
                res = make_response(jsonify({'type': 'error', 'info': '密码错误'}))
                res.headers['Access-Control-Allow-Origin'] = '*'
                res.headers['Access-Control-Allow-Method'] = '*'
                res.headers['Access-Control-Allow-Headers'] = '*'
                return res
            else:
                if type == 1:
                    # 解决跨域问题
                    res = make_response(jsonify({'type':'customer','account':account}))
                    res.headers['Access-Control-Allow-Origin'] = '*'
                    res.headers['Access-Control-Allow-Method'] = '*'
                    res.headers['Access-Control-Allow-Headers'] = '*'
                    return res
                elif type == 2:
                    # 解决跨域问题
                    res = make_response(jsonify({'type':'waiter','account':account}))
                    res.headers['Access-Control-Allow-Origin'] = '*'
                    res.headers['Access-Control-Allow-Method'] = '*'
                    res.headers['Access-Control-Allow-Headers'] = '*'
                    return res
                elif type == 3:
                    # 解决跨域问题
                    res = make_response(jsonify({'type':'host','account':account}))
                    res.headers['Access-Control-Allow-Origin'] = '*'
                    res.headers['Access-Control-Allow-Method'] = '*'
                    res.headers['Access-Control-Allow-Headers'] = '*'
                    return res
                else:
                    return None
        elif func=='register':
            account = request.form.get('account')
            pwd = request.form.get('pwd')
            pwdAffirm=request.form.get('pwdAffirm')
            type=LoginAndRegister.register(account,pwd,pwdAffirm)
            if type==-1:
                # 解决跨域问题
                res = make_response(jsonify({'type':'error','info':'该账户名已经被注册'}))
                res.headers['Access-Control-Allow-Origin'] = '*'
                res.headers['Access-Control-Allow-Method'] = '*'
                res.headers['Access-Control-Allow-Headers'] = '*'
                return res
                # return render_template('Login.html', error='该账户名已经被注册')
            elif type==0:
                # 解决跨域问题
                res = make_response(jsonify({'type':'error','info':'密码与确认密码不匹配'}))
                res.headers['Access-Control-Allow-Origin'] = '*'
                res.headers['Access-Control-Allow-Method'] = '*'
                res.headers['Access-Control-Allow-Headers'] = '*'
                return res
                # return render_template('Login.html', error='密码与确认密码不匹配')
            elif type==1:
                res = make_response(jsonify({'type':'right'}))
                res.headers['Access-Control-Allow-Origin'] = '*'
                res.headers['Access-Control-Allow-Method'] = '*'
                res.headers['Access-Control-Allow-Headers'] = '*'
                return res
                # return render_template('InfoHint.html')
            else:
                return None
        else:
            return None

@app.route('/store/customer/<account>',methods=['GET','POST'])
def customer(account):
    if request.method=='GET':
        # 用户信息加载
        customerInfo=Customer.customerInfoLoad(account)
        # 货物信息加载
        cargoInfo=Customer.cargoInfoLoadForCus(1)
        # 订单信息加载，（未结算订单）
        orderFormInfo=Customer.orderFormLoadForCus(account,0)
        return render_template("Customer.html",customerInfo=customerInfo,cargoInfo=cargoInfo,orderFormInfo=orderFormInfo)
    else:
        func=request.form.get('func')
        if func=='getNotDeleteCargoNum':
            # 传递未删除货物的总数
            num=Customer.getNotDeleteCargoNum()
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='changePage':
            # 改变页面的同时，改变面板内容
            page=request.form.get('page')
            customerInfo=Customer.cargoInfoLoadForCus(int(page))
            res = make_response(jsonify(customerInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='order':
            # 下订单
            orderDetail_json=request.form.get('orderDetail')
            orderDetail=json.loads(orderDetail_json)
            # 生成订单，并存入数据库
            num = Customer.generateOrderForm(account,orderDetail)
            # 解决跨域问题
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='modiPwd':
            # 账户
            account_modi=request.form.get('account')
            # 原密码
            oldPwd = request.form.get('oldPwd')
            # 新密码
            newPwd = request.form.get('newPwd')
            # 确认密码
            modi_affirm = request.form.get('affirmPwd')
            # 修改密码
            num = Customer.modiCustomerPwd(account_modi,oldPwd,newPwd,modi_affirm)
            # 解决跨域问题
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='modiBasicInfo':
            # 账户修改值
            accountAlter=request.form.get('accountAlter')
            # 地址修改值
            address=request.form.get('address')
            # 修改账户和地址
            num=Customer.modiCustomerBasicInfo(account,accountAlter,address)
            # 解决跨域问题
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='recharge':
            # 获得金钱数值
            money = request.form.get('money')
            # 修改金钱数值
            Customer.modiCustomerBalance(account,float(money))
            # 解决跨域问题
            res = make_response(jsonify({'response': "成功"}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getNotPaidOrderForm':
            # accountTemp = request.form.get('account')
            orderFormInfo = Customer.orderFormLoadForCus(account, 0)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getPaidOrderForm':
            # accountTemp = request.form.get('account')
            orderFormInfo = Customer.orderFormLoadForCus(account, 1)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getFinishOrderForm':
            # accountTemp = request.form.get('account')
            orderFormInfo1 = Customer.orderFormLoadForCus(account, 2)
            orderFormInfo2 = Customer.orderFormLoadForCus(account, 3)
            orderFormInfo1.update(orderFormInfo2)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo1))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getRefundOrderForm':
            # accountTemp = request.form.get('account')
            orderFormInfo = Customer.orderFormLoadForCus(account, 4)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='pay':
            orfo_no=request.form.get('orfo_no')
            # 前往结算
            num=Customer.orderFormPay(account,orfo_no)
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res

    return None

@app.route('/store/waiter/<account>',methods=['GET','POST'])
def waiter(account):
    if request.method=='GET':
        # 用户信息加载
        waiterInfo=Waiter.waiterInfoLoad(account)
        # 货物信息加载
        cargoInfo = Waiter.cargoInfoLoadForWai( 1)
        # 订单信息加载
        orderFormInfo=Waiter.orderFormLoadForWai(account,0)
        orderFormInfo1=Waiter.orderFormLoadForWai(account,1)
        orderFormInfo.update(orderFormInfo1)
        return render_template("Waiter.html",waiterInfo=waiterInfo,orderFormInfo=orderFormInfo,cargoInfo=cargoInfo)
    else:
        func=request.form.get('func')
        if func=='wai_modiPwdBtn':
            # 原密码
            oldPwd = request.form.get('oldPwd')
            # 新密码
            newPwd = request.form.get('newPwd')
            # 确认密码
            modi_affirm = request.form.get('affirmPwd')
            # 修改密码
            accountTemp=account
            # print(account)
            num = Waiter.modiWaiterPwd(accountTemp, oldPwd, newPwd, modi_affirm)
            # 解决跨域问题
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=="modiBasicInfo":
            accountAlter=request.form.get('accountAlter')
            name=request.form.get('name')
            num = Waiter.modiWaiterBasicInfo(account,accountAlter,name )
            # print(num)
            # 解决跨域问题
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getNotDealOrderForm':
            orderFormInfo = Waiter.orderFormLoadForWai(account, 0)
            orderFormInfo1 = Waiter.orderFormLoadForWai(account, 1)
            orderFormInfo.update(orderFormInfo1)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getFinishOrderForm':
            orderFormInfo = Waiter.orderFormLoadForWai(account, 2)
            # print(orderFormInfo)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getRefundOrderForm':
            orderFormInfo = Waiter.orderFormLoadForWai(account, 3)
            # print(orderFormInfo)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getAllInvolvedOrderForm':
            orderFormInfo = Waiter.orderFormLoadForWai(account, 4)
            # print(orderFormInfo)
            # 解决跨域问题
            res = make_response(jsonify(orderFormInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='dealForDeliver':
            orfo_no = request.form.get('orfo_no')
            # 前往发货
            num = Waiter.deliverCargos(account, orfo_no)
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getAllCargos':
            cargoInfo = Waiter.cargoInfoLoadForWai(1)
            # print(orderFormInfo)
            # 解决跨域问题
            res = make_response(jsonify(cargoInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getNotDelCargos':
            cargoInfo = Waiter.cargoInfoLoadForWai(2)
            # print(orderFormInfo)
            # 解决跨域问题
            res = make_response(jsonify(cargoInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='getDelCargos':
            cargoInfo = Waiter.cargoInfoLoadForWai(3)
            # print(orderFormInfo)
            # 解决跨域问题
            res = make_response(jsonify(cargoInfo))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res
        elif func=='deleteCargo':
            carg_no = request.form.get('carg_no')
            # 前往发货
            num = Waiter.deleteCargo(account, carg_no)
            res = make_response(jsonify({'num': num}))
            res.headers['Access-Control-Allow-Origin'] = '*'
            res.headers['Access-Control-Allow-Method'] = '*'
            res.headers['Access-Control-Allow-Headers'] = '*'
            return res

    return None

@app.route('/store/host')
def host():
    return render_template("Host.html")

@app.route('/store/success-register')
def demo():
    return render_template("InfoHint.html")

@app.route('/store')
def demo1():
    return render_template("InfoHint.html")




if __name__ == '__main__':
    app.run(host='127.0.0.1',port=5000)
